package com.wwu.system.controller;

import com.wwu.common.constant.CommConst;
import com.wwu.common.enums.MenuBtnEnum;
import com.wwu.common.web.ResultObj;
import com.wwu.common.web.ResultPage;
import com.wwu.system.common.aspect.Log;
import com.wwu.system.entity.SysDictType;
import com.wwu.system.service.ISysDictTypeService;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 系统字典类型前端控制器
 * @author 一蓑烟雨
 * @date 2023-07-03
 * @version 1.0.0
 */
@Api(tags = "系统字典类型控制器(SysDictTypeController)")
@ApiSort(8)
@Controller
@RequestMapping("/system/sysDictType")
public class SysDictTypeController {
    @Resource
    private ISysDictTypeService sysDictTypeService;

    /**
     * 请求转发字典类型管理主页面
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/3 9:49
     */
    @GetMapping("/dictTypeMain")
    public String dictTypeMainHtml() {
        return "system/sysdict/dict-type-main";
    }

    /**
     * 请求转发字典类型增加/编辑主页面
     * @param dictTypeId  字典类型编码
     * @param model
     * @return java.lang.String
     * @author 一蓑烟雨
     * @date 2023/7/3 9:57
     */
    @GetMapping("/dictTypeAddOrUpdate")
    public String dictTypeAddOrUpdateHtml(String dictTypeId, Model model) {
        if(StringUtils.hasText(dictTypeId)){
            model.addAttribute(CommConst.SYS_DICT_TYPE,sysDictTypeService.getById(dictTypeId));
        }
        return "system/sysdict/dict-type-add-edit";
    }

    @Log(menuName = "字典类型管理",menuBtnEnum = MenuBtnEnum.SHOW)
    @ApiOperation(value = "分页查询字典类型信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数",paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数",paramType = "query", required = true),
            @ApiImplicitParam(name = "sysDictType", value = "字典类型实体")
    })
    @GetMapping("/dictTypeList")
    @ResponseBody
    public ResultPage<SysDictType> dictTypeList(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "5000") Integer limit,
                                                SysDictType sysDictType){
        return sysDictTypeService.getDictTypeListPage(page,limit,sysDictType);
    }

    @Log(menuName = "字典类型管理",menuBtnEnum = MenuBtnEnum.ADD)
    @ApiOperation(value = "添加字典类型")
    @ApiOperationSupport(order = 2)
    @PostMapping("/saveDictType")
    @ResponseBody
    public ResultObj saveDictType(@RequestBody SysDictType sysDictType){
        sysDictTypeService.saveDictType(sysDictType);
        return ResultObj.success("字典类型添加成功");
    }

    @Log(menuName = "字典类型管理",menuBtnEnum = MenuBtnEnum.EDIT)
    @ApiOperation(value = "更新字典类型")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateDictType")
    @ResponseBody
    public ResultObj updateDictType(@RequestBody SysDictType sysDictType){
        sysDictTypeService.updateDictType(sysDictType);
        return ResultObj.success("字典类型更新成功");
    }

    @Log(menuName = "字典类型管理",menuBtnEnum = MenuBtnEnum.STATUS)
    @ApiOperation(value = "更新字典类型状态")
    @ApiOperationSupport(order = 4)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dictTypeId",value = "字典类型编码",required = true),
            @ApiImplicitParam(name = "dictTypeStatus",value = "字典类型状态(0:启用;1:作废)",required = true)
    })
    @PutMapping("/updateDictTypeStatus")
    @ResponseBody
    public ResultObj updateDictTypeStatus(String dictTypeId,Integer dictTypeStatus) {
        sysDictTypeService.updateDictTypeStatus(dictTypeId,dictTypeStatus);
        return ResultObj.success("字典类型状态更新成功");
    }

    @Log(menuName = "字典类型管理",menuBtnEnum = MenuBtnEnum.DEL)
    @ApiOperation(value = "删除字典类型")
    @ApiOperationSupport(order = 5)
    @DeleteMapping("/deleteDictType/{dictTypeIds}")
    @ResponseBody
    public ResultObj deleteDictType(@ApiParam(name = "dictTypeIds",value = "字典类型id集合，以逗号分隔",required = true)
                                        @PathVariable String dictTypeIds){
        sysDictTypeService.deleteDictType(Arrays.asList(dictTypeIds.split(CommConst.SPLIT_COMMA)));
        return ResultObj.success("字典类型删除成功");
    }
}
